var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
    var that = this;
    var lbmid = options.lbmid;
    var hospitalName = options.hospitalName;
    that.setData({
      hospitalName: hospitalName,
      lbmid: lbmid
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 记录发货信息
   */
  recordSendProductInfo:function(e) {
    var that = this;
    var postData = e.detail.value;
    postData.lbmId = this.data.lbmid;

    console.log('form发生了submit事件，携带数据为：', postData)
    var pipleCount = postData.pipleCount;
    var equCount = postData.equCount;
    var pipleIds = postData.pipleIds;
    var chairIds = postData.chairIds;

    if (!pipleCount && !equCount) {
      wx.showToast({
        title: '桩数量不能为空',
      });
      return;
    }

    if (!pipleIds && !chairIds) {
      wx.showToast({
        title: '桩或轮椅编号不能为空',
      });
      return;
    }
    if(pipleCount && !pipleIds) {
      wx.showToast({
        title: '桩信息有误',
      })
      return;
    }
    if(equCount && !chairIds) {
      wx.showToast({
        title: '轮椅信息有误',
      })
      return;
    }
    
    that.setData({
      errInfo:''
    })

    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/produce_send/record',
        header: header,
        data:postData,
        success: function (res) {
          console.log("------res",res);
          if (res.data.result === 0) {
            wx.showToast({
              title: '成功',
            })
            that.setData({
              form_info:'',
              eqNums:'',
              chairNums:'',
              errInfo:''
            })
          }else {
            var idList = res.data.data;
            var errInfo = res.data.description;
            console.log("------->errInfo",errInfo);
            if (idList) {
              errInfo += ":" + idList
            }

            console.log("---111---->errInfo", errInfo);
            that.setData({
              errInfo: errInfo
            })
            wx.showToast({
              title: "保存失败",
            })
          }
        }
      })
    });

  },


  /**
   * 录入表单清空
   */
  recordFormReset:function() {
    this.setData({
      errInfo: ''
    })
  },


  scan: function () {
    var that = this;
    wx.scanCode({
      success: function (res) {
        //记录日志
        // var logData = { reqNumber: 1, optDesc: "桩二维码前缀常量:" + ConstantData.CONSTATNT_DATA.DEVICE_QRCODE_URL_PREFIX };
        // that.connectLog(logData)

        var qrc = '';
        
        var qrCode = res.result;
        if(!qrCode){
          wx.showToast({
            title: '二维码为空',
            icon: 'none'
          })
          return;
        }
        if (qrCode.indexOf(ConstantData.CONSTATNT_DATA.DEVICE_QRCODE_URL_PREFIX) == 0) {
          //设备二维码
          var rst = qrCode.split('/');
          qrc = rst[rst.length - 1];
        } else {
          that.setData({
            alertBoxStatus: true,
            alertBoxTipContent: '二维码有误!'
          });
          return
        }


        var nophone = app.getUserLoginNoPhone();
        if (nophone == null || nophone == undefined || nophone == 1) {
          wx.setStorageSync('scan_qrc', qrc);
          wx.navigateTo({
            url: '../../../../../../../pages/login/login?pt=scan',
          })
          return
        }

        console.log("scan-----------qrc=" + qrc)
        console.log("******获取要连接的设备名称")
        that.getHomingPileId(qrc);
      },
      fail: function (res) {

      }
    });
  },


  /**
 * 获取归位桩id
 */
  getHomingPileId: function (qrc) {
    var that = this;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/equ_position/get_hid',
        header: header,
        data: {
          qrc: qrc
        },
        success: function (res) {
          console.log("hids---->", res);
          var hids = that.data.eqNums;
          var hidNum = res.data.hid;
          if ((hids + ',').indexOf(hidNum + ',') >= 0) {
            wx.showModal({
              title: '重复桩号提示',
              content: '已经扫描过' + hidNum + '号桩'
            });

            return;
          }
          if (hids && hidNum > 0) {
            var endFlg = hids.endsWith(',') || hids.endsWith('，') || hids.endsWith(' ') || hids.endsWith('.') || hids.endsWith('。');
            hids += (endFlg ? '' : ',') + hidNum;
          } else {
            hids = hidNum;
          }
          var hospital = wx.getStorageSync('setPositionHospital');
          if (!hospital) {
            hospital = res.data.hospital;
            wx.setStorageSync('setPositionHospital', hospital);
            that.setData({
              currentHospital: '当前所在医院:【' + hospital + '】'
            })
          }
          if (res.data.hospital != hospital) {
            that.setData({
              tipStatus: true,
              evtType: 1,
              tipContentColor: "color:red;",
              changeHid: hidNum,
              belongHospital: hospital,
              tipContent: hidNum + '号桩不属于:【' + hospital + "】;而属于:【" + res.data.hospital + "】"
            });
          } else {
            that.setData({
              tipStatus: true,
              evtType: 0,
              tipContent: hidNum + '号桩属于:【' + res.data.hospital + '】'
            });
          }
          console.log("-------------------hids", hids);
          that.setData({
            eqNums: hids
          })
          that.getHomingHospitals();
        }
      })
    });
  },




})